package com.example.interviewassistant;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QuestionItemAdapter extends ArrayAdapter<InterviewQuestion> {
    private LayoutInflater inflater;
    private Map<String, Integer> typeColors;
    
    public QuestionItemAdapter(Context context, List<InterviewQuestion> questions) {
        super(context, 0, questions);
        inflater = LayoutInflater.from(context);
        
        // 初始化问题类型颜色映射
        typeColors = new HashMap<>();
        typeColors.put("客观知识型", Color.parseColor("#4CAF50")); // 绿色
        typeColors.put("实践经验型", Color.parseColor("#2196F3")); // 蓝色
        typeColors.put("思维评估型", Color.parseColor("#FF9800")); // 橙色
        typeColors.put("主观开放型", Color.parseColor("#9C27B0")); // 紫色
        typeColors.put("未分类", Color.parseColor("#757575"));     // 灰色
        typeColors.put("未知", Color.parseColor("#757575"));       // 灰色
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_interview_question, parent, false);
            holder = new ViewHolder();
            holder.tvQuestion = convertView.findViewById(R.id.tvQuestion);
            holder.tvExpectedAnswer = convertView.findViewById(R.id.tvExpectedAnswer);
            holder.tvKeyPoints = convertView.findViewById(R.id.tvKeyPoints);
            holder.tvQuestionType = convertView.findViewById(R.id.tvQuestionType);
            holder.iconExpand = convertView.findViewById(R.id.iconExpand);
            holder.layoutDetails = convertView.findViewById(R.id.layoutDetails);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        
        InterviewQuestion question = getItem(position);
        if (question != null) {
            // 设置问题文本
            holder.tvQuestion.setText((position + 1) + ". " + question.getQuestion());
            
            // 设置问题类型
            String questionType = question.getQuestionType();
            if (questionType != null && !questionType.isEmpty()) {
                holder.tvQuestionType.setText(questionType);
                holder.tvQuestionType.setVisibility(View.VISIBLE);
                
                // 设置背景颜色
                Integer color = typeColors.get(questionType);
                if (color != null) {
                    holder.tvQuestionType.setBackgroundTintList(ColorStateList.valueOf(color));
                } else {
                    // 默认灰色
                    holder.tvQuestionType.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#757575")));
                }
            } else {
                holder.tvQuestionType.setVisibility(View.GONE);
            }
            
            // 设置期望回答和考察点
            holder.tvExpectedAnswer.setText(question.getExpectedAnswer());
            holder.tvKeyPoints.setText(question.getKeyPoints());
            
            // 初始状态下隐藏详细信息
            holder.layoutDetails.setVisibility(View.GONE);
            
            // 点击问题时展开/收起详细信息
            convertView.setOnClickListener(v -> {
                boolean isExpanded = holder.layoutDetails.getVisibility() == View.VISIBLE;
                holder.layoutDetails.setVisibility(isExpanded ? View.GONE : View.VISIBLE);
                holder.iconExpand.setRotation(isExpanded ? 0 : 180); // 旋转箭头图标
            });
        }
        
        return convertView;
    }
    
    private static class ViewHolder {
        TextView tvQuestion;
        TextView tvExpectedAnswer;
        TextView tvKeyPoints;
        TextView tvQuestionType;
        ImageView iconExpand;
        LinearLayout layoutDetails;
    }
} 